<?php
class Model_Search
{    
    public function search($date, $flg, $modelname, $username)
    {
        $works = Model_Work::getByTime($date);
        $ret = array();
        $days = Model_Common::getMonthDays($date);
        $smallDay = Model_Common::getStampTime("{$date}-01");
        $bigDay = Model_Common::getStampTime("{$date}-{$days}");
        $modelTimeName = Model_Model::getAllTimeName();
        for($i=1; $i<=$days; $i++){
            $ret[$i] = array();
            $nowTime = Model_Common::getStampTime("{$date}-{$i}");
            if(isset($modelTimeName[$nowTime])){
            	$ret[$i]['modelstate'] = $modelTimeName[$nowTime];
            }
        }
        
        foreach($works as $work){
            if($flg != 0 && $work['flg'] != $flg){
                continue;
            }
            if($modelname != "" && !stristr(strtolower($work['modelname']),  strtolower($modelname))){
                continue;
            }
            if($username != "" &&  strtolower($work['username']) != strtolower($username)){
                continue;
            }

            $dayStart = intval(Model_Common::getFormatTimeD($work['starttime']));

            if($work['starttime'] > $smallDay){
                $dayStart = intval(Model_Common::getFormatTimeD($work['starttime']));
            }else{
                $dayStart = intval(Model_Common::getFormatTimeD($smallDay));
            }

            if($work['endtime'] < $bigDay){
                $dayEnd = intval(Model_Common::getFormatTimeD($work['endtime']));
            }else{
                $dayEnd = intval(Model_Common::getFormatTimeD($bigDay));
            }

            for($day = $dayStart; $day <=$dayEnd; $day++){
                $temp['workid'] = $work['workid'];
                $temp['username'] = $work['username'];
                $nickname = Model_Model::getNicksByNameStr($work['modelname']);
                $temp['modelnamestr'] = str_replace(',', ', ', $nickname);
                $temp['modelname'] = array();
                if($work['modelname'] != ''){
	                $modelArr = explode(',', $work['modelname']);
	                foreach($modelArr as $name){
	                	$temp['modelname'][$name] = Model_Model::getIDByName($name);
	                }
                }else{
                	$temp['modelname'] = array();
                }
                $temp['time'] = self::formatTime($work['starthour'], $work['startmin'], $work['endhour'], $work['endmin']);
                $temp['flg'] = self::formatFlg($work['flg']);
                $temp['enflg'] = Model_Common::chs2eng($temp['flg']);
                $temp['type'] = self::formatType($work['worktype']);
                $temp['entype'] = Model_Common::chs2eng($temp['type']);
                $temp['info'] = Model_Common::addBr($work['info']);
                $temp['sure'] = $work['sure'];
                $temp['perprice'] = $work['perprice'];
                $ret[$day]['work'][] = $temp; 
            }
        }
        return $ret;
    }
    
    public static function download($date, $flg, $modelname, $username)
    {
        $works = Model_Work::getByTime($date);
        $kv['username'] = '经纪人';
        $kv['starttime'] = '开始日期';
        $kv['endtime'] = '结束日期';
        $kv['starthour'] = '开始时间';
        $kv['endhour'] = '结束时间';
        $kv['flg'] = '项目选择';
        $kv['modelname'] = '参与模特';
        $kv['info'] = '非本公司资源详情';
        $kv['worktype'] = '工作类型';
        $kv['perprice'] = '收费单价';
        $kv['companyname'] = '客户名称';
        $kv['customername'] = '客户联系人';
        $kv['customertel'] = '手机号';
        $kv['customerothertel'] = '其它联系方式';
        $kv['customeraddress'] = '客户地址';
        $kv['price'] = '实际回款金额';
        $kv['prepricetime'] = '预计回款日期';
        $kv['pricetime'] = '实际回款时间';
        $kv['cost'] = '成本';
        $kv['tax'] = '税款';
        $kv['pay'] = '提成';
        $kv['ret'] = '客户返款';
        $kv['profit'] = '利润';
        $kv['createdtime'] = '创建日期';

        $header = self::formatHeader($kv);
        $body = "";
        foreach($works as $work){
            if($flg != 0 && $work['flg'] != $flg){
                continue;
            }
            if($modelname != "" && !stristr(strtolower($work['modelname']),  strtolower($modelname))){
                continue;
            }
            if($username != "" &&  strtolower($work['username']) != strtolower($username)){
                continue;
            }
            $bodyTemp = self::formatBody($kv, $work);
            $body .= $bodyTemp;
        }
        
        return "<table style='vnd.ms-excel.numberformat:@'>" . $header . $body . "</table>";
    }
    
    public static function formatHeader($kv)
    {
        $header = implode("</td><td>", $kv);
        
        return "<tr style='text-align:center;font-weight:bold;'><td>" . $header . "</td></tr>";
    }
    
    public static function formatBody($kv, $work)
    {
        $temp = array();
        foreach($kv as $k=>$v){
        	if($k == "flg"){
        		$temp[$k] = self::formatFlg($work['flg']);
        	}else if($k == 'worktype'){
        		$temp[$k] = Model_Common::getWorkTypeName($work['worktype']);
        	}else if($k == 'starttime'){
        		$temp[$k] = Model_Common::getFormatTimeYMD($work['starttime']);
        	}else if($k == 'endtime'){
        		$temp[$k] = Model_Common::getFormatTimeYMD($work['endtime']);
        	}else if($k == 'starthour'){
        		$temp[$k] = Model_Common::getFomrtTimeHMSByHour($work['starthour'], $work['startmin']);
        	}else if($k == 'endhour'){
        		$temp[$k] = Model_Common::getFomrtTimeHMSByHour($work['endhour'], $work['endmin']);
        	}else if($k == 'pricetime'){
        		if($work['pricetime'] != 0){
        			$temp[$k] = Model_Common::getFormatTimeYMD($work['pricetime']);
        		}else{
        			$temp[$k] = '';
        		}
        	}else if($k == 'createdtime'){
        		$temp[$k] = Model_Common::getFormatTimeYMD($work['createdtime']);
        	}else if($k == 'prepricetime'){
        		$temp[$k] = Model_Common::getFormatTimeYMD($work['prepricetime']);
        	}else{
        		$temp[$k] = $work[$k];
        	}
        }
        
        $body = implode("</td><td>", $temp);     
        return "<tr><td>" . $body . "</td></tr>";
    }
    
    public static function formatFlg($id)
    {
        return $id == 1 ? '面试':'工作';
    }
    
    public static function formatType($id)
    {
        $workTypeName = Model_Common::getWorkTypeName($id);
        $workTypeArr = explode('-', $workTypeName);
        return $workTypeArr[0];
    }
    
    public static function formatTime($start, $smin, $end, $emin)
    {
        $s = Model_Common::getFomrtTimeHMSByHour($start, $smin);
        $e = Model_Common::getFomrtTimeHMSByHour($end, $emin);
        
        return $s . "-" . $e;
    }
}
